import { _decorator, Component, Node } from 'cc';
import { EvolutionStages, EvolutionSkills } from '../config/EvolutionConfig';
import { GameEvents } from '../constants/GameConstants';
import { EventManager } from '../core/EventManager';
import { Player } from '../entity/Player';
import { AudioSystem } from './AudioSystem';
import { AudioConfig } from '../config/AudioConfig';

const { ccclass, property } = _decorator;

/**
 * 进化系统
 * 
 * 功能：
 * 1. 管理生物进化
 * 2. 处理进化条件和效果
 * 3. 控制进化路线
 * 4. 管理进化点数
 * 
 * 特点：
 * 1. 多分支进化路线
 * 2. 进化条件系统
 * 3. 属性成长系统
 * 4. 技能解锁机制
 */
@ccclass('EvolutionSystem')
export class EvolutionSystem extends Component {
    @property(Player)
    private player: Player | null = null;

    private currentStage: keyof typeof EvolutionStages = 'BASIC';
    private availableStages: (keyof typeof EvolutionStages)[] = [];
    private unlockedSkills: Set<keyof typeof EvolutionSkills> = new Set();

    start() {
        this.initEvolutionSystem();
    }

    private initEvolutionSystem() {
        // 初始化可用进化阶段
        this.updateAvailableStages();

        // 监听进化点数变化
        EventManager.instance.on(GameEvents.EVOLUTION_POINT_CHANGE, this.onEvolutionPointsChange, this);
    }

    private updateAvailableStages() {
        const currentStageConfig = EvolutionStages[this.currentStage];
        this.availableStages = currentStageConfig.nextStages as (keyof typeof EvolutionStages)[];
    }

    private onEvolutionPointsChange(points: number) {
        // 检查是否可以进化
        this.checkEvolutionAvailability(points);
    }

    private checkEvolutionAvailability(points: number) {
        const availableEvolutions = this.availableStages.filter(stage => {
            return points >= EvolutionStages[stage].requiredPoints;
        });

        if (availableEvolutions.length > 0) {
            // 发送可进化通知
            EventManager.instance.emit(GameEvents.EVOLUTION_AVAILABLE, availableEvolutions);
        }
    }

    public evolve(targetStage: keyof typeof EvolutionStages) {
        if (!this.availableStages.includes(targetStage)) {
            console.error('Invalid evolution stage:', targetStage);
            return false;
        }

        const stageConfig = EvolutionStages[targetStage];
        
        // 更新玩家属性
        if (this.player) {
            this.player.updateStats({
                hp: stageConfig.hp,
                moveSpeed: stageConfig.moveSpeed,
                damage: stageConfig.damage,
                size: stageConfig.size
            });
        }

        // 解锁新技能
        this.unlockSkills(stageConfig.skills);

        // 更新当前阶段
        this.currentStage = targetStage;
        this.updateAvailableStages();

        // 播放进化音效（音量放大）
        AudioSystem.instance.playEffect(AudioConfig.EFFECTS.EVOLUTION, 1.2);

        // 可以考虑在进化过程中改变背景音乐
        AudioSystem.instance.crossFadeMusic('evolution_music', 3.0);

        // 发送进化完成事件
        EventManager.instance.emit(GameEvents.EVOLUTION_COMPLETE, targetStage);

        return true;
    }

    private unlockSkills(skills: (keyof typeof EvolutionSkills)[]) {
        skills.forEach(skill => {
            if (!this.unlockedSkills.has(skill)) {
                this.unlockedSkills.add(skill);
                // 发送技能解锁事件
                EventManager.instance.emit(GameEvents.SKILL_UNLOCKED, skill);
            }
        });
    }

    public getAvailableStages(): (keyof typeof EvolutionStages)[] {
        return this.availableStages;
    }

    public getCurrentStage(): keyof typeof EvolutionStages {
        return this.currentStage;
    }

    public getUnlockedSkills(): Set<keyof typeof EvolutionSkills> {
        return this.unlockedSkills;
    }

    onDestroy() {
        EventManager.instance.off(GameEvents.EVOLUTION_POINT_CHANGE, this.onEvolutionPointsChange, this);
    }
} 